package com.joesem.sale.client.logic.event

import android.os.Bundle
import android.view.LayoutInflater
import androidx.core.view.forEachIndexed
import androidx.fragment.app.FragmentManager
import com.google.android.material.chip.ChipGroup
import com.jiaxing.sale.client.databinding.DialogEventFilterBinding
import com.jiaxing.sale.client.databinding.ItemEventFilterBinding
import com.joesem.sale.client.base.BaseBottomFragmentDialog
import com.joesem.sale.client.base.createViewModel
import com.joesem.sale.client.base.gone
import com.joesem.sale.client.base.visible
import com.joesem.sale.client.utils.click


class EventFilterDialog(
    val result: (EventFilterMultipleMode) -> Unit,
    val filterParams: EventFilterMultipleMode?
) :
    BaseBottomFragmentDialog<DialogEventFilterBinding>() {

    private var checkType: FilterMode? = null

    private var checkDate: FilterMode? = null

    private var checkAboutMe: FilterMode? = null

    private val viewModel by createViewModel<EventViewModel>()

    override fun DialogEventFilterBinding.initView(savedInstanceState: Bundle?) {
        viewModel.getEventParams()

    }

    private fun addItem(
        data: List<FilterMode>,
        filterView: ChipGroup,
        default: FilterMode? = null,
        selectedItem: (FilterMode) -> Unit
    ) {
        data.forEachIndexed { index, mode ->
            val itemBind = ItemEventFilterBinding.inflate(LayoutInflater.from(requireContext()))
            itemBind.chip.apply {
                text = mode.title
                tag = mode
                default?.let {
                    if (it.code == mode.code && it.title == mode.title) {
                        this.isSelected = true
                        selectedItem.invoke(it)
                    }
                } ?: run {
                    if (index == 0) {
                        this.isSelected = true
                        selectedItem.invoke(mode)
                    }
                }
                click {
                    selectedItem.invoke(mode)
                    filterView.forEachIndexed { index, view ->
                        view.isSelected = it == view
                    }
                }
            }
            filterView.addView(itemBind.root)
        }
    }

    override fun DialogEventFilterBinding.initListener() {
        btnClose.click {
            dismiss()
        }
        btnReset.click {
            cgType.forEachIndexed { index, view ->
                view.isSelected = index == 0
                if (view.isSelected) {
                    checkType = view.tag as? FilterMode?
                }
            }
            cgAboutMe.forEachIndexed { index, view ->
                view.isSelected = index == 0
                if (view.isSelected) {
                    checkAboutMe = view.tag as? FilterMode?
                }
            }
            cgDate.forEachIndexed { index, view ->
                view.isSelected = index == 0
                if (view.isSelected) {
                    checkDate = view.tag as? FilterMode?
                }
            }
        }
        btnConfirm.click {
            result.invoke(
                EventFilterMultipleMode(type = checkType, about_me = checkAboutMe, date = checkDate)
            )
            dismiss()
        }
    }

    override fun DialogEventFilterBinding.initIntent(intent: Any?) {
        when (intent) {
            is EventViewModel.EventParamsIntent -> {
                bind.llContent.visible()
                intent.data.types?.filterNotNull()?.map { FilterMode(title = it, code = it) }
                    ?.let {
                        arrayListOf<FilterMode>().apply {
                            add(FilterMode(title = "全部", code = ""))
                            addAll(it)
                        }.let {
                            addItem(it, cgType, filterParams?.type) {
                                checkType = it
                            }
                        }
                    }
                addItem(intent.data.about_me, cgAboutMe, filterParams?.about_me) {
                    checkAboutMe = it
                }
                addItem(intent.data.date, cgDate, filterParams?.date) {
                    checkDate = it
                }
            }
        }
    }

    override fun showLoading() {
        bind.llLoading.visible()
        bind.llContent.gone()
    }

    override fun dismissLoading() {
        bind.llLoading.gone()

    }
}

data class FilterMode(val title: String, val code: String)
data class EventFilterMultipleMode(
    val type: FilterMode?,
    val about_me: FilterMode?,
    val date: FilterMode?
)

fun optionEventFilter(
    fm: FragmentManager,
    filterParams: EventFilterMultipleMode?,
    result: (EventFilterMultipleMode) -> Unit
) {
    EventFilterDialog(result, filterParams).show(fm, null)
}